package com.outsourcing.bottle.adapter;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.TextUtils;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.outsourcing.bottle.R;
import com.outsourcing.bottle.domain.ExFeedEntry;
import com.outsourcing.bottle.domain.ExPicFeedEntry;
import com.outsourcing.bottle.domain.ExsessionsEntry;
import com.outsourcing.bottle.remoteimage.RemoteImageView;
import com.outsourcing.bottle.ui.ChooseFriendActivity;
import com.outsourcing.bottle.ui.ExchangeInfoDetailActivity;
import com.outsourcing.bottle.ui.ExchangePicDetailActivity;
import com.outsourcing.bottle.ui.ExchangePicFeedDetailActivity;
import com.outsourcing.bottle.ui.ExpandEditActivity;
import com.outsourcing.bottle.ui.MapViewActivity;
import com.outsourcing.bottle.ui.PhotoDetailActivity;
import com.outsourcing.bottle.ui.UserSpaceActivity;
import com.outsourcing.bottle.ui.fragment.ExchangeFragment;
import com.outsourcing.bottle.util.AppContext;
import com.outsourcing.bottle.util.ImageLoader;
import com.outsourcing.bottle.util.ServiceUtils;
import com.outsourcing.bottle.util.TextUtil;

public class ExchangeTimeLineAdapter extends BaseAdapter {

	private Context context;
	private List<ExsessionsEntry> mExList = null;
	private ImageLoader imageLoader = null;
	private ExchangeFragment fragment = null;

	public ExchangeTimeLineAdapter(Context context, ExchangeFragment fragment,
			List<ExsessionsEntry> mExList) {
		this.context = context;
		this.mExList = mExList;
		this.fragment = fragment;
		imageLoader = new ImageLoader(context, AppContext.BottleTimelineIcon);
	}

	@Override
	public int getCount() {
		return mExList.size();
	}

	@Override
	public Object getItem(int position) {
		return mExList.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	ExchangeBottleHolder holder = null;

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			holder = new ExchangeBottleHolder();
			convertView = LayoutInflater.from(context).inflate(
					R.layout.bottle_list_avatar_item, null);
			holder.flag = position;
			holder.mAuthorView = (RemoteImageView) convertView
					.findViewById(R.id.iv_author_photo);
			holder.mExType = (ImageView) convertView
					.findViewById(R.id.iv_bottle_type);
			// content layout
			LinearLayout contentLayout = (LinearLayout) convertView
					.findViewById(R.id.feed_post_body);
			View view = LayoutInflater.from(context).inflate(
					R.layout.exchange_list_detail_item, null);

			holder.mNickName = (TextView) view.findViewById(R.id.tv_nickname);
			holder.mTime = (TextView) view.findViewById(R.id.tv_time);
			holder.mExAvatar = (RemoteImageView) view
					.findViewById(R.id.iv_author_photo);

			holder.mComment = (Button) view
					.findViewById(R.id.bt_exchange_comment);
			holder.mReject = (Button) view
					.findViewById(R.id.bt_exchange_reject);
			holder.mAccept = (Button) view
					.findViewById(R.id.bt_exchange_accept);
			holder.mVisiable = (Button) view
					.findViewById(R.id.bt_exchange_visible);
			holder.mInvisiable = (Button) view
					.findViewById(R.id.bt_exchange_invisible);

			holder.mfeedMoreComment = (RelativeLayout) view
					.findViewById(R.id.feed_comments_more);
			holder.mfeedNum = (TextView) view
					.findViewById(R.id.comments_ellipsis_text);
			holder.mfeedComment = (LinearLayout) view
					.findViewById(R.id.ll_feed_comment_layout);
			holder.mfeedComment.removeAllViews();
			contentLayout.addView(view);
			convertView.setTag(holder);
		} else {
			holder = (ExchangeBottleHolder) convertView.getTag();
			holder.mfeedComment.removeAllViews();
		}

		final ExsessionsEntry exsessionsEntry = (ExsessionsEntry) getItem(position);

		try {
			holder.mAuthorView.setDefaultImage(R.drawable.avatar_default_big);
			holder.mAuthorView.setImageUrl(exsessionsEntry.getExs_avatar());
		} catch (Exception e) {
			e.printStackTrace();
			holder.mAuthorView.setImageResource(R.drawable.avatar_default_big);
		}
		holder.mAuthorView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent spaceIntent = new Intent(context, UserSpaceActivity.class);
				spaceIntent.putExtra("ouid", exsessionsEntry.getExs_uid());
				context.startActivity(spaceIntent);
			}
		});
		holder.mComment.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent commentIntent = new Intent(context,
						ExpandEditActivity.class);
				Bundle commentBundle = new Bundle();
				fragment.position = position;
				fragment.exsid = exsessionsEntry.getExsid();
				if (exsessionsEntry.getExs_type() == 1) {
					commentBundle.putString("from_type",
							AppContext.REPLY_INFOEX);
					int ouid = (exsessionsEntry.getExs_ouid() != AppContext
							.getInstance().getLogin_uid()) ? exsessionsEntry
							.getExs_ouid()
							: ((exsessionsEntry.getExs_uid() != AppContext
									.getInstance().getLogin_uid()) ? exsessionsEntry
									.getExs_uid() : AppContext.getInstance()
									.getLogin_uid());
					commentBundle.putInt("ouid", ouid);
					commentBundle.putString("reply_infoex_type", "replay");
					commentIntent.putExtras(commentBundle);
				} else {
					AppContext.getInstance().setFromExchangeTime(true);
					commentBundle
							.putString("from_type", AppContext.REPLY_PICEX);
					int ouid = (exsessionsEntry.getExs_ouid() != AppContext
							.getInstance().getLogin_uid()) ? exsessionsEntry
							.getExs_ouid()
							: ((exsessionsEntry.getExs_uid() != AppContext
									.getInstance().getLogin_uid()) ? exsessionsEntry
									.getExs_uid() : AppContext.getInstance()
									.getLogin_uid());
					commentBundle.putInt("ouid", ouid);
					commentBundle.putString("reply_picex_type", "reply");
					commentIntent.putExtras(commentBundle);
				}
				context.startActivity(commentIntent);
			}
		});
		if (exsessionsEntry.getExs_type() == 1) {
			holder.mExType.setImageResource(R.drawable.btn_exprofile);
			holder.mExType.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					int ouid = (exsessionsEntry.getExs_ouid() != AppContext
							.getInstance().getLogin_uid()) ? exsessionsEntry
							.getExs_ouid()
							: ((exsessionsEntry.getExs_uid() != AppContext
									.getInstance().getLogin_uid()) ? exsessionsEntry
									.getExs_uid() : AppContext.getInstance()
									.getLogin_uid());
					fragment.doExchange(
							AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE, ouid);

				}
			});

			if (exsessionsEntry.getExs_infoex_rejectshow() == 1) {
				holder.mReject.setVisibility(View.VISIBLE);
				holder.mReject.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						int ouid = (exsessionsEntry.getExs_ouid() != AppContext
								.getInstance().getLogin_uid()) ? exsessionsEntry
								.getExs_ouid()
								: ((exsessionsEntry.getExs_uid() != AppContext
										.getInstance().getLogin_uid()) ? exsessionsEntry
										.getExs_uid() : AppContext
										.getInstance().getLogin_uid());
						fragment.doRejectInfoex(ouid, position,
								exsessionsEntry.getExsid());
					}
				});
			} else {
				holder.mReject.setVisibility(View.INVISIBLE);
			}
			if (exsessionsEntry.getExs_infoex_acceptshow() == 1) {
				holder.mAccept.setVisibility(View.VISIBLE);
				holder.mAccept.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						int ouid = (exsessionsEntry.getExs_ouid() != AppContext
								.getInstance().getLogin_uid()) ? exsessionsEntry
								.getExs_ouid()
								: ((exsessionsEntry.getExs_uid() != AppContext
										.getInstance().getLogin_uid()) ? exsessionsEntry
										.getExs_uid() : AppContext
										.getInstance().getLogin_uid());
						fragment.doAcceptInfoex(ouid, position,
								exsessionsEntry.getExsid());
					}
				});
			} else {
				holder.mAccept.setVisibility(View.GONE);
			}
			if (exsessionsEntry.getExs_infoex_visibleshow() == 1) {
				holder.mVisiable.setVisibility(View.VISIBLE);
				holder.mVisiable.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						int ouid = (exsessionsEntry.getExs_ouid() != AppContext
								.getInstance().getLogin_uid()) ? exsessionsEntry
								.getExs_ouid()
								: ((exsessionsEntry.getExs_uid() != AppContext
										.getInstance().getLogin_uid()) ? exsessionsEntry
										.getExs_uid() : AppContext
										.getInstance().getLogin_uid());
						fragment.doVisibleInfoex(ouid, position,
								exsessionsEntry.getExsid());
					}
				});
			} else {
				holder.mVisiable.setVisibility(View.GONE);
			}
			if (exsessionsEntry.getExs_infoex_invisibleshow() == 1) {
				holder.mInvisiable.setVisibility(View.VISIBLE);
				holder.mInvisiable
						.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								int ouid = (exsessionsEntry.getExs_ouid() != AppContext
										.getInstance().getLogin_uid()) ? exsessionsEntry
										.getExs_ouid()
										: ((exsessionsEntry.getExs_uid() != AppContext
												.getInstance().getLogin_uid()) ? exsessionsEntry
												.getExs_uid() : AppContext
												.getInstance().getLogin_uid());
								fragment.doInvisibleInfoex(ouid, position,
										exsessionsEntry.getExsid());
							}
						});
			} else {
				holder.mInvisiable.setVisibility(View.GONE);
			}

		} else {
			holder.mExType.setImageResource(R.drawable.btn_exphoto);
			holder.mExType.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					int ouid = (exsessionsEntry.getExs_ouid() != AppContext
							.getInstance().getLogin_uid()) ? exsessionsEntry
							.getExs_ouid()
							: ((exsessionsEntry.getExs_uid() != AppContext
									.getInstance().getLogin_uid()) ? exsessionsEntry
									.getExs_uid() : AppContext.getInstance()
									.getLogin_uid());
					fragment.doExchange(
							AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE, ouid);
				}
			});
			if (exsessionsEntry.getExs_picex_rejectshow() == 1) {
				holder.mReject.setVisibility(View.VISIBLE);
				holder.mReject.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						int ouid = (exsessionsEntry.getExs_ouid() != AppContext
								.getInstance().getLogin_uid()) ? exsessionsEntry
								.getExs_ouid()
								: ((exsessionsEntry.getExs_uid() != AppContext
										.getInstance().getLogin_uid()) ? exsessionsEntry
										.getExs_uid() : AppContext
										.getInstance().getLogin_uid());
						fragment.doRejectPicex(ouid, position,
								exsessionsEntry.getExsid());
					}
				});
			} else {
				holder.mReject.setVisibility(View.INVISIBLE);
			}
			if (exsessionsEntry.getExs_picex_visibleshow() == 1) {
				holder.mVisiable.setVisibility(View.VISIBLE);
				holder.mVisiable.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						int ouid = (exsessionsEntry.getExs_ouid() != AppContext
								.getInstance().getLogin_uid()) ? exsessionsEntry
								.getExs_ouid()
								: ((exsessionsEntry.getExs_uid() != AppContext
										.getInstance().getLogin_uid()) ? exsessionsEntry
										.getExs_uid() : AppContext
										.getInstance().getLogin_uid());
						fragment.doVisiblePicex(ouid, position,
								exsessionsEntry.getExsid());
					}
				});
			} else {
				holder.mVisiable.setVisibility(View.GONE);
			}
			if (exsessionsEntry.getExs_picex_invisibleshow() == 1) {
				holder.mInvisiable.setVisibility(View.VISIBLE);
				holder.mInvisiable
						.setOnClickListener(new View.OnClickListener() {
							@Override
							public void onClick(View v) {
								int ouid = (exsessionsEntry.getExs_ouid() != AppContext
										.getInstance().getLogin_uid()) ? exsessionsEntry
										.getExs_ouid()
										: ((exsessionsEntry.getExs_uid() != AppContext
												.getInstance().getLogin_uid()) ? exsessionsEntry
												.getExs_uid() : AppContext
												.getInstance().getLogin_uid());
								fragment.doInvisiblePicex(ouid, position,
										exsessionsEntry.getExsid());
							}
						});
			} else {
				holder.mInvisiable.setVisibility(View.GONE);
			}
		}
		try {
			holder.mExAvatar.setDefaultImage(R.drawable.avatar_default_big);
			holder.mExAvatar.setImageUrl(exsessionsEntry.getExs_oavatar());
		} catch (Exception e) {
			e.printStackTrace();
			holder.mExAvatar.setImageResource(R.drawable.avatar_default_big);
		}
		holder.mExAvatar.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent spaceIntent = new Intent(context, UserSpaceActivity.class);
				spaceIntent.putExtra("ouid", exsessionsEntry.getExs_ouid());
				context.startActivity(spaceIntent);
			}
		});

		String nickName = null;
		if (exsessionsEntry.getExs_uid() != AppContext.getInstance()
				.getLogin_uid()) {
			nickName = "<font color='#DA4A37'>"
							+ exsessionsEntry.getExs_nickname() + "</font>";
		} else {
			nickName = "<font color='#3F99D8'>"
					+ exsessionsEntry.getExs_nickname() + "</font>";
		}
		
		String mFirstFeed = (exsessionsEntry.getExs_firstfeed() != null && exsessionsEntry
				.getExs_firstfeed().length() > 0) ? " "
				+ exsessionsEntry.getExs_firstfeed() : "";

		String mSuccess = (exsessionsEntry.getExs_success() == 1) ? ("<font color='#DA4A37'>"
				+ TextUtil.R("exchange_success") + "</font>")
				: ("<font color='#DA4A37'>" + TextUtil.R("exchange_failed") + "</font>");

		String time = (exsessionsEntry.getExs_firstfeedtime() != null && exsessionsEntry
				.getExs_firstfeedtime().length() > 0) ? ("<font color='#666666'>"
				+ "#" + exsessionsEntry.getExs_firstfeedtime() + "</font>")
				: "";
		holder.mNickName.setText(Html.fromHtml(nickName + mFirstFeed + " "
				+ mSuccess));
		if (time.length() > 0) {
			holder.mTime.setVisibility(View.VISIBLE);
			holder.mTime.setText(Html.fromHtml(time));
		} else {
			holder.mTime.setVisibility(View.GONE);
		}

		List<ExFeedEntry> mExFeedEntries = null;

		if (null != exsessionsEntry.getExfeeds_list()
				&& exsessionsEntry.getExfeeds_list().size() > 0) {
			holder.mfeedMoreComment.setVisibility(View.VISIBLE);
			holder.mfeedMoreComment
					.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							int ouid = (exsessionsEntry.getExs_ouid() != AppContext
									.getInstance().getLogin_uid()) ? exsessionsEntry
									.getExs_ouid() : ((exsessionsEntry
									.getExs_uid() != AppContext.getInstance()
									.getLogin_uid()) ? exsessionsEntry
									.getExs_uid() : AppContext.getInstance()
									.getLogin_uid());
							if (exsessionsEntry.getExs_type() == 1) {
								fragment.doExchange(
										AppContext.CHOOSE_FRIEND_EXCHANGE_INFO_TYPE,
										ouid);
							} else {
								fragment.doExchange(
										AppContext.CHOOSE_FRIEND_EXCHANGE_PHOTO_TYPE,
										ouid);
							}
						}
					});
			holder.mfeedNum.setText(exsessionsEntry.getExfeeds_count() + " "
					+ TextUtil.R("bottle_txt_comment_more"));

			mExFeedEntries = exsessionsEntry.getExfeeds_list();

			for (int i = 0; i < mExFeedEntries.size(); i++) {
				final ExFeedEntry exfeedEntry = mExFeedEntries.get(i);
				View mCommentView = LayoutInflater.from(context).inflate(
						R.layout.exchange_comments_item, null);
				if (i == (mExFeedEntries.size()-1)) {
					View feed_avatar = (View) mCommentView
							.findViewById(R.id.line_exchange_feed);
					feed_avatar.setVisibility(View.INVISIBLE);
				}
				TextView mFeedAvatar = (TextView) mCommentView
						.findViewById(R.id.tv_feed_comment_nickname);
				String feed_name = null;
				if (exfeedEntry.getExfeed_uid() != AppContext.getInstance()
						.getLogin_uid()) {
					feed_name = "<font color='#DA4A37'>"
							+ exfeedEntry.getExfeed_nickname() + "</font>";
				} else {
					feed_name = "<font color='#3F99D8'>"
							+ exfeedEntry.getExfeed_nickname() + "</font>";
				}

				mFeedAvatar
						.setText(Html.fromHtml(feed_name
								+ " "
								+ TextUtil.htmlEncode(exfeedEntry
										.getExfeed_content())));
				TextView mFeedComment = (TextView) mCommentView
						.findViewById(R.id.tv_feed_comment_content);
				if (!TextUtils.isEmpty(exfeedEntry.getExfeed_comment())) {
					mFeedComment.setVisibility(View.VISIBLE);
					String commentTips = "<font color='#000000'>"
							+ TextUtil.R("exchange_feed_comment_title")
							+ "</font>";
					String feed_content = exfeedEntry.getExfeed_isnew() == 1 ? ("<font color='#DA4A37'>"
							+ TextUtil.htmlEncode(exfeedEntry
									.getExfeed_comment()) + "</font>")
							: TextUtil.htmlEncode(exfeedEntry
									.getExfeed_comment());
					mFeedComment.setText(Html.fromHtml(commentTips
							+ feed_content));
				} else {
					mFeedComment.setVisibility(View.GONE);
				}

				TextView mFeedTime = (TextView) mCommentView
						.findViewById(R.id.tv_feed_comment_time);
				if (!TextUtils.isEmpty(exfeedEntry.getExfeed_location())
						|| !TextUtils.isEmpty(exfeedEntry.getExfeed_time())) {
					mFeedTime.setVisibility(View.VISIBLE);
					String commentTime = TextUtils.isEmpty(exfeedEntry
							.getExfeed_time()) ? "" : ("#" + exfeedEntry
							.getExfeed_time());
					String commentLocation = TextUtils.isEmpty(exfeedEntry
							.getExfeed_location()) ? "" : ("@" + exfeedEntry
							.getExfeed_location());
					mFeedTime.setText(commentTime + " " + commentLocation);
					mFeedTime.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {

							final Context dialogContext = new ContextThemeWrapper(
									context, android.R.style.Theme_Light);
							String[] choices = new String[1];
							choices[0] = context
									.getString(R.string.bottle_txt_check_location);

							final ListAdapter adapter = new ArrayAdapter<String>(
									dialogContext,
									android.R.layout.simple_list_item_1,
									choices);

							final AlertDialog.Builder builder = new AlertDialog.Builder(
									dialogContext);
							builder.setTitle(R.string.system_info);
							builder.setSingleChoiceItems(adapter, -1,
									new DialogInterface.OnClickListener() {
										public void onClick(
												DialogInterface dialog,
												int which) {
											dialog.dismiss();
											Intent intent = new Intent(context,
													MapViewActivity.class);
											intent.putExtra("longitude",
													exfeedEntry.getExfeed_lng());
											intent.putExtra("latidute",
													exfeedEntry.getExfeed_lat());
											intent.putExtra(
													"location",
													exfeedEntry
															.getExfeed_location());
											context.startActivity(intent);
										}
									});
							builder.create().show();
						}
					});
				} else {
					mFeedTime.setVisibility(View.GONE);
				}
				RemoteImageView mFeedCommentAvatar = (RemoteImageView) mCommentView
						.findViewById(R.id.comment_profile_photo);
				try {
					mFeedCommentAvatar.setDefaultImage(R.drawable.avatar_default_small);
					mFeedCommentAvatar.setImageUrl(exfeedEntry.getExfeed_avatar());
				} catch (Exception e) {
					e.printStackTrace();
					mFeedCommentAvatar
							.setImageResource(R.drawable.avatar_default_small);
				}
				mFeedCommentAvatar
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								Intent spaceIntent = new Intent(context, UserSpaceActivity.class);
								spaceIntent.putExtra("ouid", exfeedEntry.getExfeed_uid());
								context.startActivity(spaceIntent);
							}
						});

				RelativeLayout exFeedPicContent = (RelativeLayout) mCommentView
						.findViewById(R.id.rl_exchange_feed_type);
				if (exfeedEntry.getExfeed_type() == 1) {
					exFeedPicContent.setVisibility(View.GONE);
				} else {
					if (!TextUtils.isEmpty(exfeedEntry.getExfeed_pic())) {
						exFeedPicContent.setVisibility(View.VISIBLE);

						RelativeLayout rl_bottle_content_photo = (RelativeLayout) mCommentView
								.findViewById(R.id.rl_bottle_content_photo);
						LinearLayout ll_bottle_photo_locked = (LinearLayout) mCommentView
								.findViewById(R.id.ll_bottle_photo_locked);
						Button comment_button_exchange = (Button) mCommentView
								.findViewById(R.id.comment_button_exchange);
						LinearLayout comment_button_like = (LinearLayout) mCommentView
								.findViewById(R.id.comment_button_like);
						if (exfeedEntry.getExfeed_pic_locked() == 1) {
							comment_button_like.setVisibility(View.GONE);
							rl_bottle_content_photo.setVisibility(View.GONE);
							ll_bottle_photo_locked.setVisibility(View.VISIBLE);
							comment_button_exchange.setVisibility(View.VISIBLE);
							comment_button_exchange
									.setOnClickListener(new View.OnClickListener() {

										@Override
										public void onClick(View v) {
											fragment.position = position;
											AppContext.getInstance()
													.setFromExchangeTime(true);
											fragment.exsid = exsessionsEntry
													.getExsid();
											Intent intentPhoto = new Intent(
													context,
													ExpandEditActivity.class);
											Bundle bundlePhoto = new Bundle();
											bundlePhoto.putString("from_type",
													AppContext.REPLY_PICEX);
											bundlePhoto.putString(
													"reply_picex_type",
													"accept");
											bundlePhoto
													.putInt("ouid", exfeedEntry
															.getExfeed_uid());
											intentPhoto.putExtras(bundlePhoto);
											context.startActivity(intentPhoto);
										}
									});
						} else {
							ll_bottle_photo_locked.setVisibility(View.GONE);
							comment_button_exchange.setVisibility(View.GONE);
							rl_bottle_content_photo.setVisibility(View.VISIBLE);
							comment_button_like.setVisibility(View.VISIBLE);
							ImageView exFeedPicView = (ImageView) mCommentView
									.findViewById(R.id.photo);
							imageLoader.DisplayImage(
									exfeedEntry.getExfeed_pic(), exFeedPicView,
									R.drawable.album_nophoto);
							exFeedPicView
									.setOnClickListener(new View.OnClickListener() {

										@Override
										public void onClick(View v) {
											int ouid = (exsessionsEntry
													.getExs_ouid() != AppContext
													.getInstance()
													.getLogin_uid()) ? exsessionsEntry
													.getExs_ouid()
													: ((exsessionsEntry
															.getExs_uid() != AppContext
															.getInstance()
															.getLogin_uid()) ? exsessionsEntry
															.getExs_uid()
															: AppContext
																	.getInstance()
																	.getLogin_uid());
											Status status = null;
											if (exfeedEntry.getExfeed_uid() == AppContext.getInstance().getLogin_uid()) {
												 
												//自己的照片
												if (exfeedEntry.getExfeed_pic_enabletuya() == 1 && exfeedEntry.getExfeed_pic_allowtuya() == 1) {
													
													status = Status.exchange_pic_feed_my_allowtuya_and_enabletuya;
													
												} else if (exfeedEntry.getExfeed_pic_enabletuya() == 2 && exfeedEntry.getExfeed_pic_allowtuya() == 1 ) {
													status = Status.exchange_pic_feed_my_allowtuya_and_not_enabletuya;
												} else {
													status = Status.exchange_pic_feed;
												}
												
												doPhotoAction(ouid,exfeedEntry, status, position, exsessionsEntry.getExsid());

											} else {
												
												if (exfeedEntry.getExfeed_pic_allowtuya() == 1) {

													status = Status.exchange_pic_feed_other_allowtuya;

												} else {
													status = Status.exchange_pic_feed_other;
												}
												
												//别人的照片
												doPhotoAction(ouid,exfeedEntry,
														status, position, exsessionsEntry.getExsid());

											}
										}
									});
							
							if (exfeedEntry.getExfeed_like_enable() == 1) {
								comment_button_like.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.photo_pill));
								comment_button_like
										.setOnClickListener(new View.OnClickListener() {

											@Override
											public void onClick(View v) {
												int ouid = (exsessionsEntry
														.getExs_ouid() != AppContext
														.getInstance()
														.getLogin_uid()) ? exsessionsEntry
														.getExs_ouid()
														: ((exsessionsEntry
																.getExs_uid() != AppContext
																.getInstance()
																.getLogin_uid()) ? exsessionsEntry
																.getExs_uid()
																: 0);
												fragment.showInfoPopupWindow(
														v,
														exfeedEntry
																.getExfeed_picid(),
														ouid, exsessionsEntry
																.getExsid(),
														position);
											}
										});
							} else {
								comment_button_like
								.setBackgroundDrawable(context.getResources().getDrawable(R.drawable.photo_pill_dark));
							}
							ImageView mLikeIcon = (ImageView) mCommentView
									.findViewById(R.id.comment_button_like_icon);

							switch (exfeedEntry.getExfeed_pic_liked()) {
							case 0:
								mLikeIcon
										.setImageResource(R.drawable.like_gray2);
								break;
							case 1:
								mLikeIcon
										.setImageResource(R.drawable.like_red2);
								break;
							case 2:
								mLikeIcon
										.setImageResource(R.drawable.like_black2);
								break;
							default:
								break;
							}
						}
					} else {
						exFeedPicContent.setVisibility(View.GONE);
					}
					List<ExPicFeedEntry> picFeedEntries = exfeedEntry
							.getExpicfeeds_list();
					if (null != picFeedEntries && picFeedEntries.size() > 0) {
						LinearLayout mCommentItemLayout = (LinearLayout) mCommentView
								.findViewById(R.id.ll_exchange_feed_comment);
//						ExPicFeedEntry exPicFeedEntry = null;
						for (int j = 0; j < picFeedEntries.size(); j++) {
							final ExPicFeedEntry exPicFeedEntry = picFeedEntries.get(j);
							View picFeedItem = LayoutInflater
									.from(context)
									.inflate(
											R.layout.exchange_pic_comments_item,
											null);
							ImageView mLink = (ImageView) picFeedItem
									.findViewById(R.id.iv_feed_comment_white);
							switch (exPicFeedEntry.getExpicfeed_type()) {
							case 0:
								mLink.setImageResource(R.drawable.feed_white);
								break;
							case 1:
								mLink.setImageResource(R.drawable.feed_white);
								break;
							case 2:
								mLink.setImageResource(R.drawable.like_red3);
								break;
							case 3:
								mLink.setImageResource(R.drawable.like_black3);
								break;
							default:
								mLink.setImageResource(R.drawable.feed_white);
								break;
							}

							TextView picFeedNickname = (TextView) picFeedItem
									.findViewById(R.id.tv_feed_comment_nickname);
							String feedex_content = exPicFeedEntry.getExpicfeed_isnew() == 1 ? ("<font color='#DA4A37'>"
									+ TextUtil.htmlEncode(exPicFeedEntry.getExpicfeed_content()) + "</font>")
									: TextUtil.htmlEncode(exPicFeedEntry.getExpicfeed_content());

							String picFeedNameStr = null;
							
							if (exPicFeedEntry.getExpicfeed_uid() != AppContext.getInstance()
									.getLogin_uid()) {
								picFeedNameStr = (!TextUtils.isEmpty(exPicFeedEntry.getExpicfeed_nickname())) ? ("<font color='#DA4A37'>"
										+ exPicFeedEntry.getExpicfeed_nickname() + "</font>"): "";
							} else {
								picFeedNameStr = (!TextUtils.isEmpty(exPicFeedEntry.getExpicfeed_nickname())) ? ("<font color='#3F99D8'>"
										+ exPicFeedEntry.getExpicfeed_nickname() + "</font>"): "";
							}
							picFeedNickname.setText(Html.fromHtml(picFeedNameStr+ " "+ feedex_content));
							
							TextView picComment = (TextView) picFeedItem
									.findViewById(R.id.tv_feed_comment_content);
							if (!TextUtils.isEmpty(exPicFeedEntry.getExpicfeed_comment())) {
								picComment.setVisibility(View.VISIBLE);
								String commentTips = "<font color='#000000'>"
										+ TextUtil
												.R("exchange_feed_comment_title")
										+ "</font>";
								picComment.setText(Html.fromHtml(commentTips
										+ TextUtil.htmlEncode(exPicFeedEntry
												.getExpicfeed_comment())));
							} else {
								picComment.setVisibility(View.GONE);
							}
							TextView picCommentTime = (TextView) picFeedItem
									.findViewById(R.id.tv_feed_comment_time);
							String picCommentLocation = TextUtils.isEmpty(exPicFeedEntry.getExpicfeed_location()) ? "" : ("@" + exPicFeedEntry.getExpicfeed_location());
							if (null != exPicFeedEntry.getExpicfeed_time()
									&& exPicFeedEntry.getExpicfeed_time()
											.length() > 0) {
								
								picCommentTime.setVisibility(View.VISIBLE);
								picCommentTime.setText("#"+exPicFeedEntry
										.getExpicfeed_time()+picCommentLocation);
								if (picCommentLocation.length()>0) {
									picCommentTime.setOnClickListener(new View.OnClickListener() {
										
										@Override
										public void onClick(View v) {

											final Context dialogContext = new ContextThemeWrapper(context,
													android.R.style.Theme_Light);
											String[] choices = new String[1];
											choices[0] = context.getString(R.string.bottle_txt_check_location);

											final ListAdapter adapter = new ArrayAdapter<String>(
													dialogContext,
													android.R.layout.simple_list_item_1,
													choices);

											final AlertDialog.Builder builder = new AlertDialog.Builder(
													dialogContext);
											builder.setTitle(R.string.system_info);
											builder.setSingleChoiceItems(adapter, -1,
													new DialogInterface.OnClickListener() {
														public void onClick(
																DialogInterface dialog,
																int which) {
															dialog.dismiss();
															Intent intent = new Intent(context,
																	MapViewActivity.class);
															intent.putExtra("longitude",exPicFeedEntry.getExpicfeed_lng());
															intent.putExtra("latidute",exPicFeedEntry.getExpicfeed_lat());
															intent.putExtra("location",exPicFeedEntry.getExpicfeed_location());
															context.startActivity(intent);
														}
													});
											builder.create().show();
									
										}
									});
								}
							}else {
								picCommentTime.setVisibility(View.INVISIBLE);
							}

							mCommentItemLayout.addView(picFeedItem);
						}
					}
				}

				holder.mfeedComment.addView(mCommentView);
				holder.mfeedComment
						.setOnClickListener(new View.OnClickListener() {

							@Override
							public void onClick(View v) {
								int ouid = (exsessionsEntry.getExs_ouid() != AppContext
										.getInstance().getLogin_uid()) ? exsessionsEntry
										.getExs_ouid()
										: ((exsessionsEntry.getExs_uid() != AppContext
												.getInstance().getLogin_uid()) ? exsessionsEntry
												.getExs_uid() : AppContext
												.getInstance().getLogin_uid());
								if (exsessionsEntry.getExs_type() == 1) {
									Intent intent = new Intent(context,
											ExchangeInfoDetailActivity.class);
									intent.putExtra("ouid", ouid);
									intent.putExtra("page", 1);
									context.startActivity(intent);
								} else {
									Intent intent = new Intent(context,
											ExchangePicDetailActivity.class);
									intent.putExtra("ouid", ouid);
									intent.putExtra("page", 1);
									context.startActivity(intent);
								}
							}
						});
			}
		} else {
			holder.mfeedMoreComment.setVisibility(View.GONE);
		}
		return convertView;
	}


	/***
	 */
	public void doPhotoAction(final int ouid, final ExFeedEntry exfeedEntry , Status status, final int position, final int exsid) {
		Context dialogContext = null;
		ListAdapter adapter = null;
		String[] choices = null;
		AlertDialog.Builder builder = null;
		switch (status) {
		case exchange_pic_feed:

			dialogContext = new ContextThemeWrapper(context, android.R.style.Theme_Light);
			choices = context.getResources().getStringArray(
					R.array.exchange_pic_feed);

			adapter = new ArrayAdapter<String>(dialogContext,
					android.R.layout.simple_list_item_1, choices);

			builder = new AlertDialog.Builder(dialogContext);
			builder.setTitle(R.string.system_info);
			builder.setSingleChoiceItems(adapter, -1,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							switch (which) {
							case 0:// View Comment
								Intent intent = new Intent(context,
										ExchangePicFeedDetailActivity.class);
								intent.putExtra("ouid", ouid);
								intent.putExtra("picid",  exfeedEntry.getExfeed_picid());
								context.startActivity(intent);
								break;
							case 1:// Comment
								fragment.position = position;
								fragment.exsid = exsid;
								Intent commentIntent = new Intent(context,
										ExpandEditActivity.class);
								AppContext.getInstance().setFromExchangeTime(
										true);
								Bundle commentBundle = new Bundle();
								commentBundle.putString("from_type",
										AppContext.REPLYPIC_PICEX);
								commentBundle.putInt("ouid", ouid);
								commentBundle.putInt("picid",  exfeedEntry.getExfeed_picid());
								commentIntent.putExtras(commentBundle);
								context.startActivity(commentIntent);
								break;
							case 2:// Cancel

								break;
							default:
								break;
							}
						}
					});
			builder.create().show();
		
			break;
		case exchange_pic_feed_my_allowtuya_and_enabletuya:
			dialogContext = new ContextThemeWrapper(context,
					android.R.style.Theme_Light);
			choices = context.getResources().getStringArray(
					R.array.exchange_pic_feed_my_allowtuya_and_enabletuya);

			adapter = new ArrayAdapter<String>(dialogContext,
					android.R.layout.simple_list_item_1, choices);

			builder = new AlertDialog.Builder(
					dialogContext);
			builder.setTitle(R.string.system_info);
			builder.setSingleChoiceItems(adapter, -1,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							switch (which) {
							case 0:// View Comment
								Intent intent = new Intent(context,
										ExchangePicFeedDetailActivity.class);
								intent.putExtra("ouid", ouid);
								intent.putExtra("picid",  exfeedEntry.getExfeed_picid());
								context.startActivity(intent);
								break;
							case 1:// Comment
								fragment.position = position;
								fragment.exsid = exsid;
								Intent commentIntent = new Intent(context, ExpandEditActivity.class);
								AppContext.getInstance().setFromExchangeTime(true);
								Bundle commentBundle = new Bundle();
								commentBundle.putString("from_type", AppContext.REPLYPIC_PICEX);
								commentBundle.putInt("ouid", ouid);
								commentBundle.putInt("picid",  exfeedEntry.getExfeed_picid());
								commentIntent.putExtras(commentBundle);
								context.startActivity(commentIntent);
								break;
							case 2://涂鸦照片
								byte[] tempBytes = null;
								try {
									String filename = ServiceUtils.convertUrlToFileName(exfeedEntry.getExfeed_pic());
									tempBytes = ServiceUtils.readStream(new FileInputStream(new File(
											android.os.Environment.getExternalStorageDirectory() + "/" + AppContext.BottleTimelineIcon + "/" + filename)));
								} catch (Exception e) {
									e.printStackTrace();
								}
								fragment.position = position;
								fragment.exsid = exsid;
								Intent bigPicIntent = new Intent(context, PhotoDetailActivity.class);
								bigPicIntent.putExtra("pic_big", exfeedEntry.getExfeed_pic_big());
								bigPicIntent.putExtra("thumbnail", tempBytes);
								bigPicIntent.putExtra("paintType", AppContext.PAINT_PICEX);
								bigPicIntent.putExtra("ouid", ouid);
								bigPicIntent.putExtra("picid", exfeedEntry.getExfeed_picid());
								bigPicIntent.putExtra("return_type",ExchangeFragment.class.getSimpleName());		
								bigPicIntent.putExtra("isToDraw", true);
								context.startActivity(bigPicIntent);	
								break;
							case 3://允许涂鸦
								fragment.doExpicPaint(ouid, exfeedEntry.getExfeed_picid(), 1, exsid, position);
								break;
							case 4:// Cancel
								break;
							default:
								break;
							}
						}
					});
			builder.create().show();
			break;
		case exchange_pic_feed_my_allowtuya_and_not_enabletuya:
			dialogContext = new ContextThemeWrapper(context,
					android.R.style.Theme_Light);
			choices = context.getResources().getStringArray(
					R.array.exchange_pic_feed_my_allowtuya_and_not_enabletuya);

			adapter = new ArrayAdapter<String>(dialogContext,
					android.R.layout.simple_list_item_1, choices);

			builder = new AlertDialog.Builder(
					dialogContext);
			builder.setTitle(R.string.system_info);
			builder.setSingleChoiceItems(adapter, -1,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							switch (which) {
							case 0:// View Comment
								Intent intent = new Intent(context,
										ExchangePicFeedDetailActivity.class);
								intent.putExtra("ouid", ouid);
								intent.putExtra("picid",  exfeedEntry.getExfeed_picid());
								context.startActivity(intent);
								break;
							case 1:// Comment
								fragment.position = position;
								fragment.exsid = exsid;
								Intent commentIntent = new Intent(context, ExpandEditActivity.class);
								AppContext.getInstance().setFromExchangeTime(true);
								Bundle commentBundle = new Bundle();
								commentBundle.putString("from_type", AppContext.REPLYPIC_PICEX);
								commentBundle.putInt("ouid", ouid);
								commentBundle.putInt("picid",  exfeedEntry.getExfeed_picid());
								commentIntent.putExtras(commentBundle);
								context.startActivity(commentIntent);
								break;
							case 2://涂鸦照片
								byte[] tempBytes = null;
								try {
									String filename = ServiceUtils.convertUrlToFileName(exfeedEntry.getExfeed_pic());
									tempBytes = ServiceUtils.readStream(new FileInputStream(new File(
											android.os.Environment.getExternalStorageDirectory() + "/" + AppContext.BottleTimelineIcon + "/" + filename)));
								} catch (Exception e) {
									e.printStackTrace();
								}
								fragment.position = position;
								fragment.exsid = exsid;
								Intent bigPicIntent = new Intent(context, PhotoDetailActivity.class);
								bigPicIntent.putExtra("pic_big", exfeedEntry.getExfeed_pic_big());
								bigPicIntent.putExtra("thumbnail", tempBytes);
								bigPicIntent.putExtra("paintType", AppContext.PAINT_PICEX);
								bigPicIntent.putExtra("ouid", ouid);
								bigPicIntent.putExtra("picid", exfeedEntry.getExfeed_picid());
								bigPicIntent.putExtra("return_type",ExchangeFragment.class.getSimpleName());		
								bigPicIntent.putExtra("isToDraw", true);
								context.startActivity(bigPicIntent);	
								break;
							case 3://不允许涂鸦
								fragment.doExpicPaint(ouid, exfeedEntry.getExfeed_picid(), 0, exsid, position);
								break;
							case 4:// Cancel
								break;
							default:
								break;
							}
						}
					});
			builder.create().show();
			break;
		case exchange_pic_feed_other:

			dialogContext = new ContextThemeWrapper(context,
					android.R.style.Theme_Light);
			choices = context.getResources().getStringArray(
					R.array.exchange_pic_feed_other);

			adapter = new ArrayAdapter<String>(dialogContext,
					android.R.layout.simple_list_item_1, choices);

			builder = new AlertDialog.Builder(
					dialogContext);
			builder.setTitle(R.string.system_info);
			builder.setSingleChoiceItems(adapter, -1,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							switch (which) {
							case 0:// View Comment
								Intent intent = new Intent(context,
										ExchangePicFeedDetailActivity.class);
								intent.putExtra("ouid", ouid);
								intent.putExtra("picid",  exfeedEntry.getExfeed_picid());
								context.startActivity(intent);
								break;
							case 1:// Comment
								fragment.position = position;
								fragment.exsid = exsid;
								Intent commentIntent = new Intent(context, ExpandEditActivity.class);
								AppContext.getInstance().setFromExchangeTime(true);
								Bundle commentBundle = new Bundle();
								commentBundle.putString("from_type", AppContext.REPLYPIC_PICEX);
								commentBundle.putInt("ouid", ouid);
								commentBundle.putInt("picid",  exfeedEntry.getExfeed_picid());
								commentIntent.putExtras(commentBundle);
								context.startActivity(commentIntent);
								break;
							case 2:
								fragment.position = position;
								fragment.exsid = exsid;
								AppContext.getInstance().setFromExchangeTime(true);
								Intent friendIntent = new Intent(context,
										ChooseFriendActivity.class);
								friendIntent.putExtra("choose_friend_type",
										AppContext.CHOOSE_FRIEND_FORWARD_PHOTO_TYPE);
								friendIntent.putExtra("picid",  exfeedEntry.getExfeed_picid());
								friendIntent.putExtra("pic_uid", ouid);
								context.startActivity(friendIntent);
								break;
							case 3:// Cancel
								break;
							default:
								break;
							}
						}
					});
			builder.create().show();
		
			break;
		case exchange_pic_feed_other_allowtuya:
			dialogContext = new ContextThemeWrapper(context,
					android.R.style.Theme_Light);
			choices = context.getResources().getStringArray(
					R.array.exchange_pic_feed_other_allowtuya);

			adapter = new ArrayAdapter<String>(dialogContext,
					android.R.layout.simple_list_item_1, choices);

			builder = new AlertDialog.Builder(
					dialogContext);
			builder.setTitle(R.string.system_info);
			builder.setSingleChoiceItems(adapter, -1,
					new DialogInterface.OnClickListener() {
						public void onClick(DialogInterface dialog, int which) {
							dialog.dismiss();
							switch (which) {
							case 0:// View Comment
								Intent intent = new Intent(context,
										ExchangePicFeedDetailActivity.class);
								intent.putExtra("ouid", ouid);
								intent.putExtra("picid",  exfeedEntry.getExfeed_picid());
								context.startActivity(intent);
								break;
							case 1:// Comment
								fragment.position = position;
								fragment.exsid = exsid;
								Intent commentIntent = new Intent(context, ExpandEditActivity.class);
								AppContext.getInstance().setFromExchangeTime(true);
								Bundle commentBundle = new Bundle();
								commentBundle.putString("from_type", AppContext.REPLYPIC_PICEX);
								commentBundle.putInt("ouid", ouid);
								commentBundle.putInt("picid",  exfeedEntry.getExfeed_picid());
								commentIntent.putExtras(commentBundle);
								context.startActivity(commentIntent);
								break;
							case 2://涂鸦照片
								byte[] tempBytes = null;
								try {
									String filename = ServiceUtils.convertUrlToFileName(exfeedEntry.getExfeed_pic());
									tempBytes = ServiceUtils.readStream(new FileInputStream(new File(
											android.os.Environment.getExternalStorageDirectory() + "/" + AppContext.BottleTimelineIcon + "/" + filename)));
								} catch (Exception e) {
									e.printStackTrace();
								}
								fragment.position = position;
								fragment.exsid = exsid;
								Intent bigPicIntent = new Intent(context, PhotoDetailActivity.class);
								bigPicIntent.putExtra("pic_big", exfeedEntry.getExfeed_pic_big());
								bigPicIntent.putExtra("thumbnail", tempBytes);
								bigPicIntent.putExtra("paintType", AppContext.PAINT_PICEX);
								bigPicIntent.putExtra("ouid", exfeedEntry.getExfeed_uid());
								bigPicIntent.putExtra("picid", exfeedEntry.getExfeed_picid());
								bigPicIntent.putExtra("return_type",ExchangeFragment.class.getSimpleName());		
								bigPicIntent.putExtra("isToDraw", true);
								context.startActivity(bigPicIntent);	
								
								break;
							case 3:
								fragment.position = position;
								fragment.exsid = exsid;
								AppContext.getInstance().setFromExchangeTime(true);
								Intent friendIntent = new Intent(context,
										ChooseFriendActivity.class);
								friendIntent.putExtra("choose_friend_type",
										AppContext.CHOOSE_FRIEND_FORWARD_PHOTO_TYPE);
								friendIntent.putExtra("picid",  exfeedEntry.getExfeed_picid());
								friendIntent.putExtra("pic_uid", ouid);
								context.startActivity(friendIntent);
								break;
							case 4:// Cancel
								break;
							default:
								break;
							}
						}
					});
			builder.create().show();
			break;
		default:
			break;
		}

	}

	
	private static enum Status{
		exchange_pic_feed,
		exchange_pic_feed_my_allowtuya_and_not_enabletuya,
		exchange_pic_feed_my_allowtuya_and_enabletuya,
		
		exchange_pic_feed_other,
		exchange_pic_feed_other_allowtuya,
	}
 	
	public class ExchangeBottleHolder {
		RemoteImageView mAuthorView;
		ImageView mExType;
		TextView mNickName;
		TextView mTime;
		RemoteImageView mExAvatar;
		Button mComment;
		Button mReject;
		Button mAccept;
		Button mVisiable;
		Button mInvisiable;
		RelativeLayout mfeedMoreComment;
		TextView mfeedNum;
		LinearLayout mfeedComment;
		int flag;
	}

}
